package cn.myschool.web;

import cn.myschool.entity.Grade;
import cn.myschool.service.GradeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//@WebServlet("/grades")
@WebServlet(name = "GradeServlet",urlPatterns = {"/grade","/gradeServlet","/grades"})
public class GradeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Grade> all = GradeService.ME.findALL();
        request.setAttribute("grades",all);
        //间接访问，服务器控制权转移。。。
        request.getRequestDispatcher("/WEB-INF/jsp/grades.jsp").forward(request,response);
//        response.setContentType("text/html;charset=UTF-8");
//        PrintWriter out = response.getWriter();
//        out.println("<!DOCTYPE html>");
//        out.println("<html>");
//        out.println("<head>");
//        out.println("<title>年级</title>");
//        out.println("</head>");
//        out.println("<body>");
//        out.println("<h1>年级信息</h1>");
//        out.println("<table border=\"1\" cellspacing='0' cellpadding='0'>");
//        for(Grade g:all){
//            out.println("<tr>");
//            out.println("<td>");
//            out.println(g.getId());
//            out.println("</td>");
//            out.println("<td>");
//            out.println(g.getGname());
//            out.println("</td>");
//            out.println("</tr>");
//        }
//        out.println("</table>");
//        out.println("</body>");
//        out.println("</html>");
    }
}
